package DFGA;

import java.util.ArrayList;
import java.util.List;

import abstractGA.GAParameter;
import foundation.fileUtil.DataCell;
import foundation.fileUtil.PropFileUtil;

public class DFGAParameter extends GAParameter {
	// 前多少个染色体
	float topPercent = 0.01f;
	float coef = 0.5f;

	public float getCoef() {
		if (Float.compare(coef, 0.5f) == 0) {
			coef = Float.parseFloat(propFileUtil.getParameterValue("coef").trim());
		}

		return coef;
	}

	public void setCoef(float coef) {

		if (Float.compare(this.coef, coef) != 0) {
			this.coef = coef;
			propFileUtil.saveParameter("coef", String.valueOf(coef).trim());
		}
	}

	public DFGAParameter() {

	}

	public DFGAParameter(String dfgaCfgFileName) {
		propFileUtil = PropFileUtil.getInstance(dfgaCfgFileName);
	}

	public float getTopPercent() {
		if (Float.compare(topPercent, 0.01f) == 0) {
			topPercent = Float.parseFloat(propFileUtil.getParameterValue("topPercent").trim());
		}
		return topPercent;
	}

	public void setTopPercent(float topPercent) {
		if (Float.compare(this.topPercent, topPercent) != 0) {
			this.topPercent = topPercent;
			propFileUtil.saveParameter("topPercent", String.valueOf(topPercent).trim());
		}
	}

	// 按一个参数一行输入GA的参数
	public List<List<DataCell>> ouPara() {
		List<List<DataCell>> paraRows = new ArrayList<List<DataCell>>();
		// 种群大小
		List<DataCell> popuSizeRow = new ArrayList<DataCell>();
		DataCell popuSizeDataCell = new DataCell();
		popuSizeDataCell.setName("popuSize");
		popuSizeDataCell.setValue(String.valueOf(getPopuSize()));
		popuSizeRow.add(popuSizeDataCell);
		paraRows.add(popuSizeRow);

		// 最大迭代次数（最大演化代数）
		List<DataCell> maxIterNumRow = new ArrayList<DataCell>();
		DataCell maxIterNumDataCell = new DataCell();
		maxIterNumDataCell.setName("maxIterNum");
		maxIterNumDataCell.setValue(String.valueOf(getMaxIterNum()));
		maxIterNumRow.add(maxIterNumDataCell);
		paraRows.add(maxIterNumRow);

		// 前百分之几的最好个体topPercent
		List<DataCell> topPercentRow = new ArrayList<DataCell>();
		DataCell topPercentDataCell = new DataCell();
		topPercentDataCell.setName("topPercent");
		topPercentDataCell.setValue(String.valueOf(getTopPercent()));
		topPercentRow.add(topPercentDataCell);
		paraRows.add(topPercentRow);
		// 用于计算全局当前变异因子和交叉概率的系数coef
		List<DataCell> coefRow = new ArrayList<DataCell>();
		DataCell coefDataCell = new DataCell();
		coefDataCell.setName("coef");
		coefDataCell.setValue(String.valueOf(getCoef()));
		coefRow.add(coefDataCell);
		paraRows.add(coefRow);

		return paraRows;
	}

}
